﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace Downloader
{
    public class AppConfigCache : ISimpleCache
    {
        private AppSettingsSection _configSection;
        private Configuration _config;

        public AppConfigCache()
        {
            _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            _configSection = _config.AppSettings;
        }

        public void Save(string name, string value)
        {
            if (!_configSection.Settings.AllKeys.Contains(name))
            {
                _configSection.Settings.Add(name, value);
            } else
            {
                _configSection.Settings[name].Value = value;
            }
        }

        public string Get(string name)
        {
            return _configSection.Settings[name].Value;
        }

        public void Save()
        {
            _config.Save(ConfigurationSaveMode.Modified);
        }
    }
}
